import Axios from 'axios'
const state = {
	rowId: '',          //修改的行id
    tableData: [],
    searchResult: [],       //查询结果
}
const mutations = {
	updateRowId(state, id) {            //更改行id
        state.rowId = id ? id : null;
    },
    updateTableData(state, arr) {
        let tableData = state.tableData;
        tableData.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => tableData.push(item));
        } else {
            tableData.splice(0);
        }
    }
}
const actions = {
	getTableData({state, commit, dispatch}) {
		Axios.get('currency/findCurrency').then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('updateTableData', res.data.data);
                    dispatch('getSearchResult');
                }
            }
        });
	},
	getSearchResult({state}) {         //获取搜索结果
        Axios.get('currency/findAllByName').then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    let data = res.data.data,
                        searchResult = state.searchResult;
                    searchResult.length = 0;
                    data.forEach(item => searchResult.push(item));
                }
            }
        });
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}